﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Von.Library;

namespace Von.Web.Framework
{
    /// <summary>
    /// 反射
    /// </summary>
    public static class Reflection
    {
        public static object CreateObject(string assemblyAndTypeName)
        {
            string[] setting = assemblyAndTypeName.Split(',');
            return CreateObject(setting[0], setting[1]);
        }
        /// <summary>
        /// 创建一个对象
        /// </summary>
        /// <param name="assembly">程序集名称，例如：VonPortal</param>
        /// <param name="typeName">类名称，例如：VonPortal.Web.VonTranslater</param>
        /// <returns></returns>
        public static object CreateObject(string assembly, string typeName)
        {
            Assembly a = Assembly.GetExecutingAssembly();
            Type supType;
            if (!a.CodeBase.EndsWith("/" + assembly + ".DLL", StringComparison.OrdinalIgnoreCase))
            {
                string[] files = Directory.GetFiles(Directory.GetCurrentDirectory() + "\\bin\\", assembly + ".DLL", SearchOption.AllDirectories);
                if (files.Length > 0)
                    a = Assembly.LoadFrom(files[0]);
                else return null;
            }
            supType = a.GetType(typeName);
            return Activator.CreateInstance(supType);
        }
        public static string GetMimeType(string fileExt)
        {
            string ext = fileExt.StartsWith('.') ? fileExt.Substring(1) : fileExt;
            if (ext.Equals("txt", StringComparison.OrdinalIgnoreCase)) return "text/plain";
            if (ext.Equals("doc", StringComparison.OrdinalIgnoreCase)) return "application/msword";
            if (ext.Equals("exe", StringComparison.OrdinalIgnoreCase)) return "application/octet-stream";
            if (ext.Equals("pdf", StringComparison.OrdinalIgnoreCase)) return "application/pdf";
            if (ext.Equals("ps", StringComparison.OrdinalIgnoreCase)) return "application/postscript";
            if (ext.Equals("xlm", StringComparison.OrdinalIgnoreCase)) return "application/vnd.ms-excel";
            if (ext.Equals("xls", StringComparison.OrdinalIgnoreCase)) return "application/vnd.ms-excel";
            if (ext.Equals("ppt", StringComparison.OrdinalIgnoreCase)) return "application/vnd.ms-powerpoint";
            if (ext.Equals("tgz", StringComparison.OrdinalIgnoreCase)) return "application/x-compressed";
            if (ext.Equals("gz", StringComparison.OrdinalIgnoreCase)) return "application/x-gzip";
            if (ext.Equals("mvb", StringComparison.OrdinalIgnoreCase)) return "application/x-msmediaview";
            if (ext.Equals("swf", StringComparison.OrdinalIgnoreCase)) return "application/x-shockwave-flash";
            if (ext.Equals("tar", StringComparison.OrdinalIgnoreCase)) return "application/x-tar";
            if (ext.Equals("zip", StringComparison.OrdinalIgnoreCase)) return "application/zip";
            if (ext.Equals("mp3", StringComparison.OrdinalIgnoreCase)) return "audio/mpeg";
            if (ext.Equals("wav", StringComparison.OrdinalIgnoreCase)) return "audio/x-wav";
            if (ext.Equals("bmp", StringComparison.OrdinalIgnoreCase)) return "image/bmp";
            if (ext.Equals("gif", StringComparison.OrdinalIgnoreCase)) return "image/gif";
            if (ext.Equals("jpe", StringComparison.OrdinalIgnoreCase)) return "image/jpeg";
            if (ext.Equals("jpeg", StringComparison.OrdinalIgnoreCase)) return "image/jpeg";
            if (ext.Equals("jpg", StringComparison.OrdinalIgnoreCase)) return "image/jpeg";
            if (ext.Equals("mp2", StringComparison.OrdinalIgnoreCase)) return "video/mpeg";
            if (ext.Equals("mpa", StringComparison.OrdinalIgnoreCase)) return "video/mpeg";
            if (ext.Equals("mpe", StringComparison.OrdinalIgnoreCase)) return "video/mpeg";
            if (ext.Equals("mpeg", StringComparison.OrdinalIgnoreCase)) return "video/mpeg";
            if (ext.Equals("mpg", StringComparison.OrdinalIgnoreCase)) return "video/mpeg";
            if (ext.Equals("mpv2", StringComparison.OrdinalIgnoreCase)) return "video/mpeg";
            if (ext.Equals("mov", StringComparison.OrdinalIgnoreCase)) return "video/quicktime";
            if (ext.Equals("qt", StringComparison.OrdinalIgnoreCase)) return "video/quicktime";
            if (ext.Equals("avi", StringComparison.OrdinalIgnoreCase)) return "video/x-msvideo";
            return "application/octet-stream";
        }
    }
    //public class scratchline
    //{
    //    public List<KeyValue> GetFileds(DbConnection Connection, string tableName)
    //    {
    //        List<KeyValue> _Fields = new List<KeyValue>();
    //        try
    //        {
    //            Connection.GetSchema();
    //            string[] restrictionValues = new string[4];
    //            restrictionValues[0] = null; // Catalog
    //            restrictionValues[1] = null; // Owner
    //            restrictionValues[2] = tableName; // Table
    //            restrictionValues[3] = null; // Column

    //            using (DataTable dt = Connection.GetSchema(SqlClientMetaDataCollectionNames.Columns, restrictionValues))
    //            {
    //                foreach (DataRow dr in dt.Rows)
    //                {
    //                    KeyValue field = new KeyValue();
    //                    field.Key = dr["column_name"].ToString();
    //                    field.Value = dr["data_type"].ToString();
    //                    _Fields.Add(field);
    //                }
    //            }
    //        }
    //        catch (Exception ex)
    //        {
    //            throw ex;
    //        }
    //        return _Fields;
    //    }
    //}
}